﻿/*
 * Created by SharpDevelop.
 * User: artem
 * Date: 27.04.2011
 * Time: 18:46
 * 
 */
using System;
using System.Collections;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;

using WinForms = System.Windows.Forms;

namespace Demon
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class NotificationWnd : Window
	{
        private const string resource_path = @".\Resource\icons.resx";
        private Controller controller = null;
        private IWatcher watcher = null;
		private WinForms.NotifyIcon tray_icon = new WinForms.NotifyIcon();
		private ContextMenu tray_menu = null;
		
		public NotificationWnd()
		{
			InitializeComponent();
			
			tray_icon.Click += (object sender, EventArgs e) => 
			{
				this.tray_menu.IsOpen = true;
			};

            try
            {
                ResXResourceReader reader = new ResXResourceReader(resource_path);
                IDictionaryEnumerator dict = reader.GetEnumerator();
                dict.MoveNext();
                tray_icon.Icon = dict.Value as System.Drawing.Icon;
                tray_icon.Visible = true;
            }
            catch
            {
                NotifyAboutError();
                this.Close();
            }
		}
		
		private void RunMenuItem_Click(object sender, RoutedEventArgs e)
		{
            try
            {
                controller.RunClient();
            }
            catch
            {
                NotifyAboutError();
                this.Close();
            }
		}
		
		private void StopMenuItem_Click(object sender, RoutedEventArgs e)
		{
            try
            {
                watcher.Stop();
                this.tray_icon.Visible = false;
                this.tray_menu.IsOpen = false;
            }
            catch { }
            finally
            { this.Close(); }
		}
		
		private void Window_SourceInitialized(object sender, EventArgs e)
		{
			tray_menu = Resources[@"TrayMenu"] as ContextMenu;
		}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                controller = Controller.Instance;

                watcher = controller.Watcher;

                watcher.AddNotificationHandle(this.ShowNotification);

                watcher.Run();
            }
            catch
            {
                NotifyAboutError();
                this.Close();
            }
        }

        private void ShowNotification(int id)
        {
            Action<object, object> action = 
                new Action<object, object>((object ignored1, object ignored2) =>
                {
                    try
                    {
                        NotificationDialog dlg = new NotificationDialog();

                        var ev = controller.GetEventInfo(id);

                        dlg.TitleEvent = ev.Title;
                        dlg.DescriptionEvent = ev.Description;
                        dlg.DateEvent = ev.Date;
                        dlg.Type = ev.Type;
                        dlg.Periodic = ev.Periodic;

                        dlg.Show();
                    }
                    catch { }
                });

            this.Dispatcher.Invoke(action, 
                                   System.Windows.Threading.DispatcherPriority.Normal, 
                                   null, 
                                   null);
        }

        public void NotifyAboutError()
        {
            MessageBox.Show(@"Sorry, an error occurred. Refer to the developers.",
                            @"Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
        }
	}
}